package com.radiobattletoads.player;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentSender;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.IntentSender.SendIntentException;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class PlayButton implements OnClickListener,OnTouchListener {
	
    Toast toast;

	public void onClick(View v) {
		ImageView w = (ImageView) v;
		TextView tv = (TextView)w.getRootView().findViewById(R.id.status);
		switch(PlayerService.player_status){
			case RadioBattletoadsActivity.PLAYER_STOPPED:
			case RadioBattletoadsActivity.PLAYER_DROPPED:
			case RadioBattletoadsActivity.PLAYER_IOEXCEPTION:
			case RadioBattletoadsActivity.PLAYER_NOPLAYLIST:
			case RadioBattletoadsActivity.PLAYER_BLOCKED:
				RadioBattletoadsActivity.instantiatedActivity.startService(new Intent(w.getContext(), PlayerService.class));
				break;
			case RadioBattletoadsActivity.PLAYER_PLAYING:
			case RadioBattletoadsActivity.PLAYER_BUFFERING:
				w.setImageBitmap(RadioBattletoadsActivity.playbm);
				tv.setText(R.string.status_stopped);
				PlayerService.player_status = RadioBattletoadsActivity.PLAYER_STOPPED;
				PlayerService.destroyed_by_clicking_stop = true;
				RadioBattletoadsActivity.instantiatedActivity.stopService(new Intent(w.getContext(), PlayerService.class));
				break;
			case RadioBattletoadsActivity.PLAYER_INITIAL_BUFFERING:
			case RadioBattletoadsActivity.PLAYER_CHECKINGCONNECTION:
				toast = Toast.makeText(w.getContext(), R.string.toast_cantstop, Toast.LENGTH_SHORT);
				toast.show();
				break;
		}
	}

	public boolean onTouch(View v, MotionEvent event) {
		ImageView w = (ImageView) v;
		switch(PlayerService.player_status){
			case RadioBattletoadsActivity.PLAYER_STOPPED:
			case RadioBattletoadsActivity.PLAYER_DROPPED:
			case RadioBattletoadsActivity.PLAYER_IOEXCEPTION:
			case RadioBattletoadsActivity.PLAYER_NOPLAYLIST:
			case RadioBattletoadsActivity.PLAYER_BLOCKED:
				if(event.getAction()==MotionEvent.ACTION_DOWN||event.getAction()==MotionEvent.ACTION_POINTER_DOWN){
					w.setImageBitmap(RadioBattletoadsActivity.playbm_hover);
				}
				else{
					w.setImageBitmap(RadioBattletoadsActivity.playbm);
				}
				break;
			case RadioBattletoadsActivity.PLAYER_PLAYING:
			case RadioBattletoadsActivity.PLAYER_BUFFERING:
			case RadioBattletoadsActivity.PLAYER_INITIAL_BUFFERING:
			case RadioBattletoadsActivity.PLAYER_CHECKINGCONNECTION:
				if(event.getAction()==MotionEvent.ACTION_DOWN||event.getAction()==MotionEvent.ACTION_POINTER_DOWN){
					w.setImageBitmap(RadioBattletoadsActivity.stopbm_hover);
				}
				else{
					w.setImageBitmap(RadioBattletoadsActivity.stopbm);
				}
				break;
		}
		return false;
	}
	
	static public void updateButtonAndStatus(){
		Message m = new Message();
		m.what = PlayerService.player_status;
		Log.d("player","Status: "+PlayerService.player_status);
		if(RadioBattletoadsActivity.instantiatedActivity!=null){
			RadioBattletoadsActivity.instantiatedActivity.h.sendMessage(m);
		}
	}

}
